package cn.com.ebidding.business.service.impl;

import cn.com.ebidding.business.entity.User;
import cn.com.ebidding.business.entity.UserGroup;
import cn.com.ebidding.business.enums.UserStatusEnum;
import cn.com.ebidding.business.service.SysGroupService;
import cn.com.ebidding.business.service.SysUserGroupService;
import cn.com.ebidding.business.service.SysUserService;
import cn.com.ebidding.business.vo.GroupConditionVO;
import cn.com.ebidding.business.vo.UserGroupConditionVO;
import cn.com.ebidding.framework.object.ResponseVO;
import cn.com.ebidding.persistence.beans.SysGroup;
import cn.com.ebidding.persistence.beans.SysUser;
import cn.com.ebidding.persistence.beans.SysUserGroup;
import cn.com.ebidding.persistence.mapper.SysGroupMapper;
import cn.com.ebidding.persistence.mapper.SysUserGroupMapper;
import cn.com.ebidding.persistence.mapper.SysUserMapper;
import cn.com.ebidding.util.ResultUtil;
import cn.com.ebidding.util.SessionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.omg.SendingContext.CodeBasePackage.URLSeqHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户
 *
 * @author 扫地僧 (saods@ebidding.com.cn)
 * @version 1.0
 * @website https://ksp.ebidding.com.cn
 * @date 2018/4/16 16:26
 * @since 1.0
 */
@Service
public class SysUserGroupServiceImpl implements SysUserGroupService {

    @Autowired
    private SysUserGroupMapper userGroupMapper;
    @Autowired
    private SysUserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserGroup insert(UserGroup group) {
        Assert.notNull(group, "UserGroup不可为空！");
        group.setUpdateTime(new Date());
        group.setCreateTime(new Date());
        group.setStatus(UserStatusEnum.NORMAL.getCode());
        group.setUpdateUser(SessionUtil.getUser().getId());
        group.setCreateUser(SessionUtil.getUser().getId());
        userGroupMapper.insertSelective(group.getUserGroup());
        return group;
    }

/*    @Override
    public UserGroup getByUserName(String name) {
        UserGroup group = new UserGroup();
        group.setName(name);
        SysUserGroup sysGroup = this.userGroupMapper.selectOne(group.getUserGroup());
        return null == sysGroup ? null : new UserGroup(sysGroup);
    }*/

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertList(List<UserGroup> users) {
        Assert.notNull(users, "UserGroups不可为空！");
        List<SysUserGroup> sysGroups = new ArrayList<>();
        for (UserGroup group : users) {
            group.setUpdateTime(new Date());
            group.setCreateTime(new Date());
            group.setStatus(UserStatusEnum.NORMAL.getCode());
            group.setUpdateUser(SessionUtil.getUser().getId());
            group.setCreateUser(SessionUtil.getUser().getId());
            sysGroups.add(group.getUserGroup());
        }
        userGroupMapper.insertList(sysGroups);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return userGroupMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSelective(UserGroup group) {
        Assert.notNull(group, "UserGroup不可为空！");
        group.setUpdateTime(new Date());
        group.setUpdateUser(SessionUtil.getUser().getId());
        return userGroupMapper.updateByPrimaryKeySelective(group.getUserGroup()) > 0;
    }

    @Override
    public UserGroup getByPrimaryKey(Long primaryKey) {
        Assert.notNull(primaryKey, "PrimaryKey不可为空！");
        SysUserGroup sysUser = userGroupMapper.selectByPrimaryKey(primaryKey);
        return null == sysUser ? null : new UserGroup(sysUser);
    }

    @Override
    public List<UserGroup> listAll() {
        List<SysUserGroup> sysGroups = userGroupMapper.selectAll();

        if (CollectionUtils.isEmpty(sysGroups)) {
            return null;
        }
        List<UserGroup> groups = new ArrayList<>();
        for (SysUserGroup sysGroup : sysGroups) {
            groups.add(new UserGroup(sysGroup));
        }
        return groups;
    }

    @Override
    public PageInfo<UserGroup> findPageBreakByCondition(UserGroupConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<SysUserGroup> sysGroups = userGroupMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(sysGroups)) {
            return null;
        }
        List<UserGroup> groups = new ArrayList<>();
        for (SysUserGroup su : sysGroups) {
            groups.add(new UserGroup(su));
        }
        PageInfo bean = new PageInfo<SysUserGroup>(sysGroups);
        bean.setList(groups);
        return bean;
    }

    @Override
    public UserGroup findUser(UserGroup userGroup) {
        SysUserGroup group = this.userGroupMapper.selectOne(userGroup.getUserGroup());
        return null == userGroup ? null : new UserGroup(group);
    }

    @Override
    public ResponseVO add(UserGroup userGroup){

        SysUser sysUser = userMapper.selectByPrimaryKey(userGroup.getUserId());
        if(sysUser == null){
            throw  new RuntimeException("无法找到该用户！");
        }
        userGroup.setName(sysUser.getNickname());
        userGroup.setStatus(1);
        userGroup.setCreateUser(SessionUtil.getUser().getId());
        this.insert(userGroup);
        return ResultUtil.success("成功");
    }


}
